package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {



    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomAttrValueService attrValueService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomInfoMapper baseMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;





    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        // 判断是否为新增或修改操作
        Long roomSubmitVoId = roomSubmitVo.getId();
        if (roomSubmitVoId != null) {

            // 删除关系表
            // 删除房间图片信息
            QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
            graphInfoQueryWrapper.eq("Item_type", 2);
            graphInfoQueryWrapper.eq("item_id", roomSubmitVoId);
            graphInfoService.remove(graphInfoQueryWrapper);
            // 删除配套信息列表
            QueryWrapper<RoomFacility> roomFacilityQueryWrapper = new QueryWrapper<>();
            roomFacilityQueryWrapper.eq("room_id", roomSubmitVoId);
            roomFacilityService.remove(roomFacilityQueryWrapper);

            // 删除属性信息列表
            QueryWrapper<RoomAttrValue> attrValueQueryWrapper = new QueryWrapper<>();
            attrValueQueryWrapper.eq("room_id", roomSubmitVoId);
            attrValueService.remove(attrValueQueryWrapper);

            // 删除标签信息列表
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomSubmitVoId);
            roomLabelService.remove(roomLabelQueryWrapper);

            // 删除支付方式列表
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomSubmitVoId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

            // 删除可选租期列表
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id",roomSubmitVoId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        }

        // 保存房间基本信息（RoomInfo）功能
        super.saveOrUpdate(roomSubmitVo);

        // 保存新的graphInfoList
        // 先取出数据
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            // 创建GraphInfo对象
            GraphInfo graphInfo = new GraphInfo();
            // 设置属性
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            // 添加进list
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

        // 保存新的facilityInfoList
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            RoomFacility roomFacility = RoomFacility.builder().build();
            roomFacility.setRoomId(roomSubmitVo.getId());
            roomFacility.setFacilityId(facilityInfoId);
            roomFacilities.add(roomFacility);
        }
        roomFacilityService.saveBatch(roomFacilities);

        // 保存新的roomAttrValueList
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
        for (Long attrValueId : attrValueIds) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().build();
            roomAttrValue.setRoomId(roomSubmitVo.getId());
            roomAttrValue.setAttrValueId(attrValueId);
            roomAttrValues.add(roomAttrValue);
        }
        attrValueService.saveBatch(roomAttrValues);

        // 保存新的labelInfoList
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        ArrayList<RoomLabel> roomLabels = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel roomLabel = RoomLabel.builder().build();
            roomLabel.setRoomId(roomSubmitVo.getId());
            roomLabel.setLabelId(labelInfoId);
            roomLabels.add(roomLabel);
        }
        roomLabelService.saveBatch(roomLabels);

        // 保存新的paymentTypeList
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().build();
            roomPaymentType.setRoomId(roomSubmitVo.getId());
            roomPaymentType.setPaymentTypeId(paymentTypeId);
            roomPaymentTypes.add(roomPaymentType);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        // 保存新的leaseTermList
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().build();
            roomLeaseTerm.setRoomId(roomSubmitVo.getId());
            roomLeaseTerm.setLeaseTermId(leaseTermId);
            roomLeaseTerms.add(roomLeaseTerm);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);


    }

    @Override
    public IPage<RoomItemVo> pageItem(Page<Object> page, RoomQueryVo queryVo) {
        IPage<RoomItemVo> list = baseMapper.selectRoomItemByQuery(page, queryVo);
        return list;
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        RoomInfo roomInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        // 所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        // 图片列表
        List<GraphVo> graphVoList =  graphInfoMapper.selectBYRoomId(id);
        // 属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        // 配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        // 标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
        // 支付方式信息列表
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.selectListByRoomId(id);
        // 租期信息列表
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.selectListByRoomId(id);

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    @Override
    public void removeByRoomId(Long id) {
        // 删除房间基本信息
        this.removeById(id);

        // 删除图片列表
        QueryWrapper<GraphInfo> graphInfoQueryWrapper = new QueryWrapper<>();
        graphInfoQueryWrapper.eq("item_id", id);
        graphInfoQueryWrapper.eq("item_type", ItemType.ROOM);
        graphInfoService.remove(graphInfoQueryWrapper);
        // 删除配套信息列表
        QueryWrapper<RoomFacility> facilityInfoQueryWrapper = new QueryWrapper<>();
        facilityInfoQueryWrapper.eq("room_id", id);
        roomFacilityService.remove(facilityInfoQueryWrapper);
        // 删除属性信息列表
        QueryWrapper<RoomAttrValue> attrValueQueryWrapper = new QueryWrapper<>();
        attrValueQueryWrapper.eq("room_id", id);
        attrValueService.remove(attrValueQueryWrapper);
        // 删除标签信息列表
        QueryWrapper<RoomLabel> labelInfoQueryWrapper = new QueryWrapper<>();
        labelInfoQueryWrapper.eq("room_id", id);
        roomLabelService.remove(labelInfoQueryWrapper);
        // 删除支付方式信息列表
        QueryWrapper<RoomPaymentType> paymentTypeQueryWrapper = new QueryWrapper<>();
        paymentTypeQueryWrapper.eq("room_id", id);
        roomPaymentTypeService.remove(paymentTypeQueryWrapper);
        // 删除租期信息列表
        QueryWrapper<RoomLeaseTerm> leaseTermQueryWrapper = new QueryWrapper<>();
        leaseTermQueryWrapper.eq("room_id", id);
        roomLeaseTermService.remove(leaseTermQueryWrapper);
    }

    @Override
    public List<Long> getStatusIds(Long id) {
        List<Long> statusIds = baseMapper.selectStatusIds(id);
        return statusIds;
    }
}




